package com.lmy.springCloud.contorller;

import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class CustomUrlCleaner implements UrlCleaner {

    /**
     * 资源清洗
     * @param originUrl 请求原url  如/member/get/10
     * @return
     */
    @Override
    public String clean(String originUrl) {
        //isBlank()就是判断 originUrl != null 且有长度且不是全部为空格
        if (StringUtils.isBlank(originUrl)) {
            return originUrl;
        }
        //如果url以 /getMemberById 开头，就进行处理
        if (originUrl.startsWith("/getMemberById")) {
            /*
                1.如果请求的接口是/getMemberById开头，如/getMemberById/1
                2.就给sentinel返回资源名为 /getMemberById/*
                3.这时做流量控制只需在sentinel 对 /getMemberById/* 添加流控规则即可
             */
            return "/getMemberById/*";
        }
        return originUrl;
    }
}
